package br.com.supermercado.web.bean;

import java.io.Serializable;
import java.util.List;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.datamodel.DataModel;
import org.jboss.seam.annotations.datamodel.DataModelSelection;

import br.com.supermercado.dao.SupermercadoDAO;
import br.com.supermercado.domain.Supermercado;

@Name("supermercadoBean")
@Scope(ScopeType.CONVERSATION)
public class SupermercadoBean implements Serializable {

	private static final long serialVersionUID = 8504620160955971252L;

	@DataModel
	private List<Supermercado> supermercadoList;

	@In(create = true)
	private SupermercadoDAO supermercadoDAO;

	@DataModelSelection
	private Supermercado supermercado;

	@Factory(value = "supermercadoList")
	public void prepareSupermercado() {
		supermercado = new Supermercado();
		supermercadoList = supermercadoDAO.loadAll();

	}

	public String prepareCreate() {
		supermercado = new Supermercado();
		return "create";

	}

	public String create() {
		supermercadoDAO.create(supermercado);
		supermercado = new Supermercado();
		supermercadoList = supermercadoDAO.loadAll();

		return "ok";

	}

	public String update() {
		supermercadoDAO.update(supermercado);
		supermercado = new Supermercado();
		supermercadoList = supermercadoDAO.loadAll();
		
		return "ok";

	}

	public String delete() {
		supermercadoDAO.delete(supermercado);
		supermercado = new Supermercado();
		supermercadoList = supermercadoDAO.loadAll();
		
		return "ok";
		
	}
	
	public Supermercado getSupermercado() {
		return supermercado;
	}

	public void setSupermercado(Supermercado supermercado) {
		this.supermercado = supermercado;
	}


}
